import axios from 'axios'
import {
	ElMessage,
	ElNotification as Notify
} from 'element-plus'
import {
	allowRequest,
	stopRepeatRequest
} from './clearHttp'
import {
	getCach
} from './cach'
import MD5 from 'md5'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const CancelToken = axios.CancelToken;

let $httpRequestList = []
window.$httpRequestList = $httpRequestList
// import qs from 'qs'

// 正在进行中的请求列表
$httpRequestList = [];

const http = axios.create({
	timeout: 300000
	// transformRequest: [data => {
	// 	return qs.parse(data);
	// }],
})

http.interceptors.request.use(config => {
	if (config._loading) {
		NProgress.start()
	}
	return config;
}, error => {
	Promise.resolve(error);
	NProgress.done()
})

http.interceptors.response.use(response => {
	NProgress.done()
	const {
		_msg,
		api_id
	} = response.config;
	allowRequest(api_id)
	if (response.data.data && response.data.data.token) {
		localStorage.setItem('token', response.data.data.token)
	}
	if (response.config.url.indexOf('/user/detail') != -1) {
		return response.data
	}
	// console.log(response)
	if (response.data.status != 200) {
		if (response.data.success == true) return response.data
		if (response.data.code == 200) return response.data
		switch (response.data.status) {

			case 1001:
				return Notify.warning({
					title: '提示',
					dangerouslyUseHTMLString: true,
					message: `<span style="word-break:break-all">登录后才可以操作哦</span>`
				})
			case -500:
				return Notify.error({
					title: '错误',
					dangerouslyUseHTMLString: true,
					message: `<span style="word-break:break-all">${response.data.errMsg || '服务器连接错误！'}</span>`
				})
			default:
				Promise.reject(response.data.message || response.message || '未知异常');
				return Notify.error({
					title: '错误',
					dangerouslyUseHTMLString: true,
					message: `<span style="word-break:break-all">${response.data.errMsg || response.message || '服务器连接错误！'}</span>`
				})
		}
	}

	if (_msg) {
		ElMessage.success({
			showClose: true,
			message: response.data.message || _msg,
			type: 'success'
		})
	}
	return response.data
}, error => {
	if (!axios.isCancel(error)) {
		Promise.reject(error);
		Notify.error({
			title: '错误',
			message: `${error}` || '服务器开小差啦！'
		})
		allowRequest(error.config.api_id)
	}
	NProgress.done()
});

export default {
	$GET(url, params = {}, config = {}) {
		const token = localStorage.getItem('token')
		if (token) {
			config = {
				...config,
				headers: {
					token
				}
			}
		}
		params.timer = new Date().getTime() // 规避IE缓存
		const api_id = MD5(JSON.stringify({
			url,
			params,
			methods: 'GET'
		}));
		stopRepeatRequest(api_id)
		return http.get(url, {
			params,
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName(api_id, c)
			})
		})
	},

	$POST(url, params, config) {
		const token = localStorage.getItem('token')
		if (token) {
			config = {
				...config,
				headers: {
					token
				}
			}
		}
		const api_id = MD5(JSON.stringify({
			url,
			params,
			methods: 'POST'
		}));
		stopRepeatRequest(api_id)
		return http.post(url, params, {
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName(api_id, c)
			})
		})
	},

	$DELETE(url, params, config) {
		const token = localStorage.getItem('token')
		if (token) {
			config = {
				...config,
				headers: {
					token
				}
			}
		}
		const api_id = MD5(JSON.stringify({
			url,
			params,
			methods: 'DELETE'
		}));
		stopRepeatRequest(api_id)
		return http.delete(url, {
			data: params,
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName({
					url,
					params,
					methods: 'DELETE'
				}, c)
			})
		})
	},

	$PUT(url, params, config) {
		const token = localStorage.getItem('token')
		if (token) {
			config = {
				...config,
				headers: {
					token
				}
			}
		}
		const api_id = MD5(JSON.stringify({
			url,
			params,
			methods: 'PUT'
		}));
		stopRepeatRequest(api_id)
		return http.put(url, params, {
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName(api_id, c)
			})
		})
	},

	$ALL() {
		return axios.all(arguments)
	}
}


function setClearFnName(api_id, c) {
	let clearFn = {};
	clearFn[api_id] = c;
	$httpRequestList.push(clearFn)
}